// pages/addemail/addemail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxNumber: 2000, //可输入最大字数
    number: 0, //已输入字数
    content: "",
    check: false,
    fabuflag:false
  },
  checkboxChange: function(e) {
    
    console.log("长度:" + e.detail.value.length);
    if(e.detail.value.length==0){
      this.setData({
        check: false
      })
    }
    else{
      this.setData({
        check: true
      })
    }


  },
  /**
   * 获取内容s
   */

  getDataBindTap: function (e) {
    var result = e.detail.value;
    let len = result.length; //获取textarea的内容长度
    this.setData({
      'number': len,
      content: result
    })
    console.log(result)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  btnClick: function () {

    if (this.data.content) {
      if(this.data.fabuflag==true){
        return
      }
      this.setData({
        fabuflag:true
      })
      this.commentPost()
    } else {
      wx.showToast({
        title: "邮件内容不能为空哦",
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }
  },


  commentPost: function () {
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: "正在发送...",
    })
    var that = this;
    var isAnonymity = 0
    if (this.data.check) {
      isAnonymity = 1
    }
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "isAnonymity": isAnonymity,
      "content": this.data.content,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_email"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "提交成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.setData({
            show: true,
            commentValue: ""
          });

          wx.navigateBack({
            url: '../yuanzhangemail/yuanzhangemail',
          })


        } else {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          wx.showToast({
            title: "提交失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.setData({
            fabuflag:false
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        wx.showToast({
          title: "提交失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        that.setData({
          fabuflag:false
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },




})